import {
    GetCompany,
    GetTemplate,
    AddOneCheck,
    GetAssessList,
    GetCheckedCompany,
    GetGetDetailData,
    DeleteDetailData,
    EditDetailData,
    GetCheckfiles,
    DeleteFile
} from '@/services/municipal';

const AssessmentNodeList =  
[
    { nodeName: '雨水管道', key: '1' },
    { nodeName: '道路桥梁', key: '2' },
];
export default {
    namespace: 'assessment',
    state: {
        AssessmentNodeList:AssessmentNodeList,
        companyList: null,
        pipingTemplateList:null,
        roadTemplateList:null,
        AssessList:[],
        PaginationChangeData:null,
        CheckedCompany:null,
        chechedkCompanyChildren:null,
        Checkfiles:null
    },
    effects: {

        // 获取养护单位
        *fetchGetCompany({ payload, callback }, { call, put }) {
            const response = yield call(GetCompany, payload);
            if (response.length) {
                yield put({
                    type: 'company',
                    payload: response,
                });

            } else {
                if (callback) callback(response);
            }
        },
        // 获取模版
        *fetchGetTemplate({ payload, callback }, { call, put }) {
            const response = yield call(GetTemplate, payload);
            
            if (response.data2.length&&payload.type==1) {
                
                yield put({
                    type: 'pipingTemplate',
                    payload: response,
                });

            }else if(response.data2.length&&payload.type==2)
            {
                yield put({
                    type: 'roadTemplate',
                    payload: response,
                });
            } 
            else {
                if (callback) callback(response);
            }
        },
        //新增
        *fetchAddOneCheck({ payload, callback }, { call, put }) {
            
            const response = yield call(AddOneCheck, payload);
            if (callback) callback(response);
            
        },
        *fetchAssessList({ payload, callback }, { call, put }) {
            const response = yield call(GetAssessList, payload);
            if (response.data.length||response.data.length==0) {
                yield put({
                    type: 'AssessList',
                    payload: response,
                });

            } else {
                if (callback) callback(response);
            }
        },
        *fetchGetCheckedCompany({ payload, callback }, { call, put }) {
            const response = yield call(GetCheckedCompany, payload);
            if (response.data.length) {
                if(payload.type=="1"){
                    yield put({
                        type: 'CheckedCompanyForPip',
                        payload: response,
                    });
                }
                else{
                    yield put({
                        type: 'CheckedCompanyForRoad',
                        payload: response,
                    });
                }
                yield put({
                    type: 'CheckedCompany',
                    payload: response,
                });

            } else {
                if (callback) callback(response);
            }
        },
        *fetchGetChechedkCompanyChildren({ payload, callback }, { call, put }) {
            const response = yield call(GetGetDetailData, payload);
            if (response.data.length||response.data.length==0) {
                yield put({
                    type: 'chechedkCompanyChildren',
                    payload: response,
                });

            } else {
                if (callback) callback(response);
            }
        },
        *fetchGetCheckfiles({ payload, callback }, { call, put }) {
            const response = yield call(GetCheckfiles, payload);
            if (response.data.length||response.data.length==0) {
                yield put({
                    type: 'Checkfiles',
                    payload: response,
                });

            } else {
                if (callback) callback(response);
            }
        },
        *DeleteDetailData({ payload, callback }, { call, put }) {
            
            const response = yield call(DeleteDetailData, payload);
            if (callback) callback(response);
            
        },
        *DeleteFile({ payload, callback }, { call, put }) {
            
            const response = yield call(DeleteFile, payload);
            if (callback) callback(response);
            
        },
        
        *EditDetailData({ payload, callback }, { call, put }) {
            
            const response = yield call(EditDetailData, payload);
            if (callback) callback(response);
            
        },
        *PaginationChangeData({ payload, callback }, { call, put }){
            // const response = yield call(null, null);
            
            // debugger
            yield put({
                type: 'savePaginationChangeData',
                payload: payload,
            });

        }
    },
    reducers: {
        //养护单位
        company(state, action) {

            return {
                ...state,
                companyList: action.payload,
            };

        },
        pipingTemplate(state, action) {

            return {
                ...state,
                pipingTemplateList: action.payload,
            };

        },
        CheckedCompanyForPip(state, action){
            
            const arr = state.pipingTemplateList;
            let sum = 0 ;
            debugger
            for (let item of arr.data2){
                for (let itemOfCheckedCompany of action.payload.data){
                    
                    if(itemOfCheckedCompany.khlx ==item.mblx&&itemOfCheckedCompany.zbbm==item.zbbm)
                    {
                    if(parseFloat(itemOfCheckedCompany.zbkf)>=parseFloat(item.zbzf))item.hasMinusedNum=0;
                    else item.hasMinusedNum = parseFloat(item.zbzf)-parseFloat(itemOfCheckedCompany.zbkf);
                    item.MinusedNum = itemOfCheckedCompany.zbkf;
                    }
                }
             
                sum = sum + (item.hasMinusedNum==0||item.hasMinusedNum? item.hasMinusedNum:item.zbzf);
            }
            arr.data2.push({hasMinusedNum:sum,px:14,khzb:'归总得分'});
            console.log(arr.data2)
            return {
                ...state,
                pipingTemplateList: arr,
            };

        },
        CheckedCompanyForRoad(state, action){
            
            const arr = state.roadTemplateList;
            let sum = 0 ;
            for (let item of arr.data2){
                for (let itemOfCheckedCompany of action.payload.data){
                    
                    if(itemOfCheckedCompany.khlx ==item.mblx&&itemOfCheckedCompany.zbbm==item.zbbm)
                    {
                    if(parseFloat(itemOfCheckedCompany.zbkf)>parseFloat(item.zbzf))item.hasMinusedNum=0;
                    else item.hasMinusedNum = parseFloat(item.zbzf)-parseFloat(itemOfCheckedCompany.zbkf);
                    item.MinusedNum = itemOfCheckedCompany.zbkf;
                    }
                }
                sum = sum + (item.hasMinusedNum? item.hasMinusedNum:item.zbzf);
            }
            arr.data2.push({hasMinusedNum:sum,px:14,flkhzb:'归总得分'});
            // console.log(arr)
            return {
                ...state,
                roadTemplateList: arr,
            };

        },
        roadTemplate(state, action) {

            return {
                ...state,
                roadTemplateList: action.payload,
            };

        },
        AssessList(state, action) {

            return {
                ...state,
                AssessList: action.payload,
            };

        },
        CheckedCompany(state, action) {
            
            return {
                ...state,
                CheckedCompany: action.payload,
            };

        },
        chechedkCompanyChildren(state, action) {
            
            return {
                ...state,
                chechedkCompanyChildren: action.payload,
            };

        },
        savePaginationChangeData(state, action){
            
            return {
                ...state,
                PaginationChangeData: action.payload,
            };

        },
        Checkfiles(state, action){
            
            return {
                ...state,
                Checkfiles: action.payload,
            };

        },
    }
}